﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.HoSoChiTietService;
namespace eTMS.Controllers{
public class HoSoChiTietControllerBase : CommonController{
	public HoSoChiTietControllerBase()
		: base()
	{	
		this.dao = new HoSoChiTietServiceClient();
	}
	~HoSoChiTietControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private HoSoChiTietEntity oHoSoChiTiet;	
	public HoSoChiTietEntity HoSoChiTietObject{
		get{
			return oHoSoChiTiet;
		}
		set{
			oHoSoChiTiet=value;
		}
	}
	protected HoSoChiTietServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSHoSoChiTiet(false);			
			return _Table;
		}
	}
	public void LoadAllDSHoSoChiTiet(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oHoSoChiTiet==null)return false;
		bool result=true;
		try{
			oHoSoChiTiet = dao.SaveRet(oHoSoChiTiet);
		}
		catch{
			result=false;
		}
		if(result){
			_ListHoSoChiTiet.Add(oHoSoChiTiet);
		}
		return result;
	}
	public bool Update(){
		if(oHoSoChiTiet==null)return false;
		return dao.Update(oHoSoChiTiet);
	}
	public bool Delete(){
		if(oHoSoChiTiet==null)return false;
		bool retvalue = dao.Delete(oHoSoChiTiet);
		if(retvalue){			
			_ListHoSoChiTiet.Remove(oHoSoChiTiet);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 HoSoChiTiet_ID){
		bool retvalue = dao.DeleteByID(HoSoChiTiet_ID);		
		if(retvalue){
			HoSoChiTietEntity oDeleted=FindHoSoChiTiet(HoSoChiTiet_ID);
			if(oDeleted!=null){
				_ListHoSoChiTiet.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oHoSoChiTiet = new HoSoChiTietEntity();
	}
    //Copy data from data row in grid to object
    public HoSoChiTietEntity MapDataRowToObject(DataRow row){
		HoSoChiTietEntity o = new HoSoChiTietEntity();
		 if(!row.IsNull("SoLuongYeuCau"))
			o.SoLuongYeuCau=ConvertInt(row["SoLuongYeuCau"]);
		 if(!row.IsNull("BatBuoc"))
			o.BatBuoc=ConvertBool(row["BatBuoc"]);
		 if(!row.IsNull("HoSo_ID"))
			o.HoSo_ID=(Int32)(row["HoSo_ID"]);
		 if(!row.IsNull("LoaiGiayTo_ID"))
			o.LoaiGiayTo_ID=(Int32)(row["LoaiGiayTo_ID"]);
		 if(!row.IsNull("HoSoChiTiet_ID"))
		    o.HoSoChiTiet_ID=(Int32)(row["HoSoChiTiet_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(HoSoChiTietEntity o,DataRow row){
		row["SoLuongYeuCau"]=o.SoLuongYeuCau;
		row["BatBuoc"]=o.BatBuoc;
		row["HoSo_ID"]=o.HoSo_ID;
		row["LoaiGiayTo_ID"]=o.LoaiGiayTo_ID;
		row["HoSoChiTiet_ID"]=o.HoSoChiTiet_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.HoSoChiTietObject,row);
    }
	public void LoadObject(Int32 HoSoChiTiet_ID){
		HoSoChiTietEntity oLoad=FindHoSoChiTiet(HoSoChiTiet_ID);
		if(oLoad==null){
			oHoSoChiTiet=this.LoadHoSoChiTiet(HoSoChiTiet_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oHoSoChiTiet.IsNullHoSoChiTiet_ID;
			if(!bNotIsNull){
				this.ListHoSoChiTiet.Add(oHoSoChiTiet);
			}
		}
		else{
			oHoSoChiTiet=oLoad;
		}
	}
	#region CachedList HoSoChiTiet
	private static List<HoSoChiTietEntity> _ListHoSoChiTiet=new List<HoSoChiTietEntity>();
    public List<HoSoChiTietEntity>ListHoSoChiTiet{
		get{
			return _ListHoSoChiTiet;
		}
	}
	public static List<HoSoChiTietEntity>ListHoSoChiTietStatic{
		get{
			return _ListHoSoChiTiet;
		}
	}
	public HoSoChiTietEntity FindHoSoChiTiet(Int32 HoSoChiTiet_ID){
		HoSoChiTietEntity oResult=this.ListHoSoChiTiet.Find(delegate(HoSoChiTietEntity oFind){
			bool result=true; 
			result&=oFind.HoSoChiTiet_ID.Equals(HoSoChiTiet_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllHoSoChiTiet(bool clearCache){
		if(_ListHoSoChiTiet.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		HoSoChiTietServiceClient dao = new HoSoChiTietServiceClient();
		_ListHoSoChiTiet=new List<HoSoChiTietEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public HoSoChiTietEntity LoadHoSoChiTiet(Int32 HoSoChiTiet_ID){			
		HoSoChiTietEntity o=dao.SelectOne(HoSoChiTiet_ID);		
		return o;
	}
	#endregion
}
}
